package com.finger.gulimall.order.config;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.finger.gulimall.order.entity.vo.PayVo;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Log4j2
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

	//在支付宝创建的应用的id
	private String app_id = "2021000118658580";

	// 商户私钥，您的PKCS8格式RSA2私钥
	private String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC4K2QUzKzo80NWebgZl51rmXhuh0/Hmzf0/LV5qvJ5BL5oO5bC0sOf3YqDqM/rcVXxsctRJlC71bVEGqbi9oS064W1t8KYq/cTJQ4tsn1Q5WCW3lyI89ctSFPZ+VOCkOXcMMJFcxprytXB2tDJMOzhWfY0aldNiqr+Gk/KxGnS70Xq2MoYIjDh0dwBmzqE322j2DpJ3FLcoHPjd7bqciTMNUKOzvn69CSlCGqi09Qo/iIhspdEIpLcvRriy5v6CwFQlYNX2Wp0v6SSLOyJwpKKFVMwOnmLZn0/vcSvaCnkhdEZnB56JoL1sKYBfK11Gl4drZVYLBsrk98egVL9wtpLAgMBAAECggEBAITOLWZeTdXSDd3e+iXiUg9UZSb3Bk2/HXPZg5fP8PfZtuB9D/M/fvfaDtdwNTbnzHz0o9PJ2HAK5KLOwUdrUzkPmaT3dSkY4Jt6niXPz1Q1hBhnEr1jcOhjfqvxggnmKQtGUeE45nWSl+ApxWM/l6HNeKBFit0MwM9p894ZEuKalIAOSPdRWD+PI0oH/Vgw+/a9bg8GxXSewa8boLhxunThhIt2drO5u3feU5scMtnGOC3QQUKzWrDGFbDsHFBxFbj9Azz5+zt6KnB+yAs1DG2vksjoqyP4H+KfLHpD1LheHy99fEmYzRSXkUySS6p0Se9GJ6n/VmfdZ0DeA352yFkCgYEA6d6Jw+rEG8by0r7V4wu6nnzBD206NqVEPxyk+Fhs3OJmIQzffs2WuHCmIeMYEV8CqjFGlZ/3NDKJT+xijOgp5XzsGgmIh/vOh9Zrgos9mg6LXg2UN3U+I18qaADC87O2fpbLoxTNr17I5t1IN9iKAWgC9H4rZlfUXOA1C4Ei5w0CgYEAyZjhQTllmoDkYi9HddyCu6RubMR63f61X4A9EyZ/O8Ed94xD/BxQvvYunr2kv/eHF0HR9sfQqSRULwerfbw8LX25IVCKh+HVanEHA3vYfGBH36CeS4Iz7Ch8RN3sQrVLEdhvxNPlUSsuHQ9UhWdC5GKXcRmrEqn7/9FbKF0icLcCgYEA3ZcN0aRPzEHlMem/nzEt0Z1DD+rejYhp8aKibOzbuS6KtiYMXYlOldOmldUmqM7tJ3Uo+/xlrf90O1YuynPvvpFu4/zEcJQWTH7NvxNPevjs9GDJXwKnrbwt4f1WurI86BTZPdwoR1H36Uchdkil+k8i/aXOWVo+zQFjQVNKTsECgYEAsbxrvEmSXqmMsVPluO/1AuJxyMthWB+cYNp6/ktOmawlc37i4X9FYVdCuwmQGv9sxwBs0RpRyQ4SenRUqcIvfBwAAHM1758XvYRxHo0tzFpGg87kQMZGaMs45K6UAiw8Rzt/LxVydCGRwmUlq4fi3eBejISI4yei2WQG7otONfkCgYA/bbatXsn9hzQLjxZ/9GEW9bz8QhMOYVs0GDPJqcKnCL8S0Y9lPviMyc+Dlg+KzOohxv+/z+2W4JJnImcp1Bp6MpE/zEZ/8dB7IM6jRzoDN52aVpTF51TR40pb/mIZFlxAX3NZ66wkScX3wVQmJJ1L6mjBBWym0NFji6fwh6ZqDg==";
	// 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
	private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt5qYUVjY62vxZRQRepc+TnmHffiCPaiiyi64Y7jdJG/J0j6q5Vs8DB37HHlo3mmxy3HBR/4faYa7voSvt4+mwwHZkzzURZyFMYilN5P1FN80Wd+dclAE4t5mJgPLLP+TW1q7BFidAejlaXhrWPcqUEV5wPrYMn0ANcZxwT9Ot0Lm6EahAGLTT27bmi6WPCupEt4Q0I3y32KwohWuyAGitBZooOQsC6xMjZaZsjxb15+AawFbFy1ppM1hvYrTXhmXpWBeFAIZqosrZz08KzjvNoHiBXJ4YRcRx9vQDUEOj9OXHkfGCTuZVMtSeJmFtfFXpUNIRrhRJOP/4DofV0cJkwIDAQAB";
	// 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
	// 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
	private String notify_url = "http://vptskm.natappfree.cc/payed/notify";

	// 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
	//同步通知，支付成功，一般跳转到成功页
	private String return_url = "http://member.gulimall.com/orderList.html";

	// 签名方式
	private String sign_type = "RSA2";

	// 字符编码格式
	private String charset = "utf-8";

	// 支付宝网关； https://openapi.alipaydev.com/gateway.do
	private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

	// 告诉支付宝这个账单多久不能支付
	private String time_expire = "1m";

	public String pay(PayVo vo) throws AlipayApiException {

		//AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
		//1、根据支付宝的配置生成一个支付客户端
		AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset, alipay_public_key, sign_type);

		//2、创建一个支付请求 //设置请求参数
		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
		alipayRequest.setReturnUrl(return_url);
		alipayRequest.setNotifyUrl(notify_url);

		//商户订单号，商户网站订单系统中唯一订单号，必填
		String out_trade_no = vo.getOut_trade_no();
		//付款金额，必填
		String total_amount = vo.getTotal_amount();
		//订单名称，必填
		String subject = vo.getSubject();
		//商品描述，可空
		String body = vo.getBody();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("out_trade_no", out_trade_no);
		jsonObject.put("total_amount", total_amount);
		jsonObject.put("subject", subject);
		jsonObject.put("body", body);
		jsonObject.put("timeout_express", time_expire);
		jsonObject.put("product_code", "FAST_INSTANT_TRADE_PAY");
		log.info(jsonObject.toJSONString());
		alipayRequest.setBizContent(jsonObject.toJSONString());

		String result = alipayClient.pageExecute(alipayRequest).getBody();

		//会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
		System.out.println("支付宝的响应：" + result);

		return result;

	}
}
